
#include <iostream>
#include <windows.h>
#include <GL/glut.h>
#include <math.h>
using namespace std;


int size;
bool _circle = false;
int mainMenu;
int colorMenu;
int shapeMenu;

float _red;
float _green;
float _blue;
int n;
float ridus;
float Pi = 3.1415926f;

int windowWidth=800;
int	windowHeight=600;


void init(){

	size = 10;
	_circle = false;
	mainMenu=1;
	colorMenu=2;
	shapeMenu=3;
	_red=_green=_blue=0;
	n = 20;
	ridus = 5;
	glClearColor(1,1,1,1);
	glColor3f(0.0,0.0,0.0);

	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glOrtho(0, windowWidth, 0, windowHeight, -1.0, 1.0);  // this is map the the windows style coordinate system, (0,0 is start from upper-left corner)
	//glOrtho(0, glutGet(GLUT_WINDOW_WIDTH), 0,glutGet(GLUT_WINDOW_HEIGHT), -1, 1);  // this is map the the windows style coordinate system, (0,0 is start from upper-left corner)


}


void resize(int width,int height)
{
	glViewport(0, 0, width, height);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glOrtho(0, glutGet(GLUT_WINDOW_WIDTH), 0,glutGet(GLUT_WINDOW_HEIGHT), -1, 1);

}


void drawSquare(int x, int y)
{
	y=glutGet(GLUT_WINDOW_HEIGHT)-y; /* invert y position */


	glColor3f(_red, _green,_blue);
	glBegin(GL_POLYGON);
	glVertex2f(x+size, y+size);
	glVertex2f(x-size, y+size);
	glVertex2f(x-size, y-size);
	glVertex2f(x+size, y-size);
	glEnd();
	glFlush();
	//glutSwapBuffers();


}


void drawCircle(int x, int y){
	y=glutGet(GLUT_WINDOW_HEIGHT)-y;
	int i;
	glColor3f(_red, _green,_blue);
	glBegin(GL_POLYGON);
	for(i=0; i<n; ++i) // I iterately draw GL_POLYGON to represent the cycle
		glVertex2f(x+ridus*cos(2*Pi/n*i), y+ridus*sin(2*Pi/n*i));
	glEnd();
	glFlush();
}

void mouse(int button, int state, int x, int y) {


	if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){
		if(_circle) drawCircle(x,y);
		else drawSquare(x, y);
	}

}



void motion(int x,   int y) { //control the draw function when mouse button down and move

	if(_circle) drawCircle(x,y);
	else drawSquare(x, y);

}


void display() {

	glClear(GL_COLOR_BUFFER_BIT);//Clear the screen:A must have or you'll lose the background customization.
	glEnd();
	//	glutSwapBuffers();
	glFlush();
}


void mykey(unsigned char key, int x, int y) //used to clear the canvas
{
	if((key == 'C') | (key == 'c')) {
		glutPostRedisplay();
	}
}


void menu(int id){


	switch(id)
	{case 3:
	glutPostRedisplay();
	break;
	case 4:
		exit(0);
		break;
	}
}

void colormenu(int id){

	switch(id)
	{
	case 1:
		_red = 0;
		_green = 0;
		_blue =0;

		break;
	case 2:
		_red = 1;
		_green = 0;
		_blue =0;

		break;
	case 3:
		_red = 0;
		_green = 1;
		_blue =0;
		break;
	case 4:
		_red = 0;
		_green = 0;
		_blue =1;

	}
}

void shapemenu(int id){
	switch(id)
	{
	case 1:
		_circle= false;
		size =10;
		break;
	case 2:
		_circle= false;
		size =20;
		break;
	case 3:
		_circle= false;
		size =40;
		break;
	case 4:
		_circle= true;
		ridus =5;
		break;
	case 5:
		_circle= true;
		ridus =10;
		break;
	case 6:
		_circle= true;
		ridus =20;
		break;
	}

}

int main(int argc, char** argv){
	glutInit(&argc,argv);

	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	//	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(windowWidth,windowHeight);
	glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH) - windowWidth)/2,(glutGet(GLUT_SCREEN_HEIGHT) - windowHeight)/2); // Set the windows centered in screen based on native resolution.
	glutCreateWindow("Assignment1 by Zhiyi Wu");

	glutReshapeFunc(resize);
	glutDisplayFunc(display);
	glutMotionFunc(motion);
	glutMouseFunc(mouse);
	glutKeyboardFunc(mykey);



	colorMenu = glutCreateMenu(colormenu); //create color menu
	glutAddMenuEntry("Black",1);
	glutAddMenuEntry("Red",2);
	glutAddMenuEntry("Green",3);
	glutAddMenuEntry("Blue",4);
	glutAttachMenu(GLUT_RIGHT_BUTTON);

	shapeMenu =  glutCreateMenu(shapemenu); //create shape menu
	glutAddMenuEntry("Small square",1);
	glutAddMenuEntry("Medium square",2);
	glutAddMenuEntry("Large square",3);
	glutAddMenuEntry("Small circle",4);
	glutAddMenuEntry("Medium circle",5);
	glutAddMenuEntry("Large circle",6);
	glutAttachMenu(GLUT_RIGHT_BUTTON);

	mainMenu=glutCreateMenu(menu);// create main menu
	glutAddSubMenu("Color", colorMenu);
	glutAddSubMenu("Shape", shapeMenu);
	glutAddMenuEntry("Clear Screen", 3);
	glutAddMenuEntry("Exit", 4);
	glutAttachMenu(GLUT_RIGHT_BUTTON);

	init();
	glutMainLoop();
return 0;

}
